package com.example.intenttrial;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class BitmapOptimization extends Activity {

	private ImageView imgvNoOpt;
	private ImageView imgvOpt;
//	private String path = Environment.getExternalStorageDirectory().getPath() + "/test";
	private String path = "/sdcard/test.jpg/";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.screen_bitmamoptim);

		
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		imgvNoOpt = (ImageView) findViewById(R.id.imgv_nooptim);
		imgvOpt = (ImageView) findViewById(R.id.imgv_optim);
		
		Bitmap noOpt = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);//f[f[f(imageFile.getAbsolutePath());
		System.out.println("--------------noOpt.getByteCount() " + noOpt.getByteCount());
		
//		Bitmap opt = decodeSampledBitmapFromPath(200, 200);
//		System.out.println("--------------opt.getByteCount() " + opt.getByteCount());
		
		BitmapWorkerTask task = new BitmapWorkerTask(imgvOpt);
		task.execute();
		
		imgvNoOpt.setImageBitmap(noOpt);
//		imgvOpt.setImageBitmap(opt);
	}

	class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
	    private final WeakReference<ImageView> imageViewReference;

	    public BitmapWorkerTask(ImageView imageView) {
	        // Use a WeakReference to ensure the ImageView can be garbage collected
	        imageViewReference = new WeakReference<ImageView>(imageView);
	    }

	    // Decode image in background.
	    @Override
	    protected Bitmap doInBackground(Integer... params) {
	        return decodeSampledBitmapFromPath(50, 50);
	    }

	    // Once complete, see if ImageView is still around and set bitmap.
	    @Override
	    protected void onPostExecute(Bitmap bitmap) {
	        if (imageViewReference != null && bitmap != null) {
	            final ImageView imageView = imageViewReference.get();
	            if (imageView != null) {
	                imageView.setImageBitmap(bitmap);
	            }
	        }
	    }
	}
	
	private Bitmap decodeSampledBitmapFromPath(int reqWidth, int reqHeight) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, options);
		
		if(bmp != null)
			System.out.println("-----------------" + bmp.getByteCount());
		else 
			System.out.println("------------------null");
		
		int realWidth = options.outWidth;
		int realHeight = options.outHeight;
		int sampleSize = 1;
		
		if(realHeight > reqHeight || realWidth > reqWidth) {
			int heightRatio = realHeight / reqHeight;
			int widthRatio = realWidth / reqWidth;
			sampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		System.out.println("------------sampleSize " + sampleSize);
		sampleSize = 5;
		options.inSampleSize = sampleSize;
		
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, options);
	}
	
}
